package com.li.youhappygo.service.Impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.util.DateUtils;
import com.li.youhappygo.exception.GlobalException;
import com.li.youhappygo.model.vo.common.ResultCode;
import com.li.youhappygo.property.MinioProperty;
import com.li.youhappygo.service.FileUploadService;
import io.minio.*;
import io.minio.errors.MinioException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;

@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    private MinioProperty minioProperty;
    @Override
    public String uploadFile(MultipartFile multipartFile) {
        try {
            // Create a minioClient with the MinIO server playground, its access key and secret key.
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint(minioProperty.getEndpoint())
                            .credentials(minioProperty.getAccessKey(), minioProperty.getSecretKey())
                            .build();

            // Make 'asiatrip' bucket if not exist.
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperty.getBucketName()).build());
            if (!found) {
                // Make a new bucket called 'asiatrip'.
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperty.getBucketName()).build());
            } else {
                System.out.println("Bucket 'asiatrip' already exists.");
            }

            // Upload '/home/user/Photos/asiaphotos.zip' as object name 'asiaphotos-2015.zip' to bucket
            // 'asiatrip'.


            String dateDir = DateUtil.format(new Date(), "yyyyMMdd");
            String uuid = UUID.randomUUID().toString().replace("-", "");
            //20230801/443e1e772bef482c95be28704bec58a901.jpg
            String fileName = dateDir+"/"+uuid+multipartFile.getOriginalFilename();
            System.out.println(fileName);

            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(minioProperty.getBucketName())
                    .stream(multipartFile.getInputStream(), multipartFile.getSize(), -1)
                    .object(fileName)
                    .build();
            minioClient.putObject(putObjectArgs) ;
            return minioProperty.getEndpoint() + "/" + minioProperty.getBucketName() + "/" + fileName ;

        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
            System.out.println("HTTP trace: " + e.httpTrace());
        } catch (IOException e) {
            throw new GlobalException(ResultCode.DATA_ERROR);
        } catch (NoSuchAlgorithmException e) {
            throw new GlobalException(ResultCode.DATA_ERROR);
        } catch (InvalidKeyException e) {
            throw new GlobalException(ResultCode.DATA_ERROR);
        }
//        String url = minioProperty.getEndpoint()+"/"+minioProperty.getBucketName()+"/"+
//                DateUtil.format(new Date(),"yyyyMMdd")
//                +UUID.randomUUID().toString().replace("-","")
//                +multipartFile.getOriginalFilename();
//        return url;
        return "error";
    }
}
